<?php

/**
 * Description of common
 *
 * @author Kishore
 */
//require_once('../config/config.php');

class ihelper {

    // returns true if the given email is valid
    public function validate_email($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    }

    function GetError($field, $arr) {
        
    }

    // handles html entities in the given key
    function SafeDisplay($key) {
        if (empty($_POST[$key])) {
            return'';
        }
        return htmlentities($_POST[$key], ENT_COMPAT, 'UTF-8');
    }

    //Redirects to given URL
    function RedirectToURL($url) {
        header("Location: $url");
        exit;
    }

    //Inputs language strings from selected language.
    function lang($key, $markers = NULL) {
        global $lang;
        if ($markers == NULL) {
            $str = $lang[$key];
        } else {
            //Replace any dyamic markers
            $str = $lang[$key];
            $iteration = 1;
            foreach ($markers as $marker) {
                $str = str_replace("%m" . $iteration . "%", $marker, $str);
                $iteration++;
            }
        }

        //Ensure we have something to return
        if ($str == "") {
            return ("No language key found");
        } else {
            return $str;
        }
    }

    // returns the message from $errs array related to $field
    function getErrorMessage($errs, $key) {
        $errMsg = '';
        if (isset($errs) && is_array($errs)) {
            if (array_key_exists($key, $errs))
                $errMsg = $errs[$key];
        }
        
        if (isset($errMsg)) {
            $errMsg = "<span class='error'>" . $errMsg . "</span>";
        }

        return $errMsg;
    }
    
    function getSessionVal($var) {
        $retVal = 'not found';
        
        if (session_id() == '') {
            // session isn't started
            session_start();
        }        
        if (array_key_exists($var, $_SESSION)) 
            $retVal = $_SESSION[$var];
        
        return $retVal;
    }

}

?>
